package com.ship.dispatch.service.fob.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ship.dispatch.bean.ConShipping;
import com.ship.dispatch.bean.SpDispatchSchemeExecute;
import com.ship.dispatch.bean.boats.ShipDashboarDataRq;
import com.ship.dispatch.bean.fob.*;
import com.ship.dispatch.bpm.utils.JsonUtils;
import com.ship.dispatch.bpm.vo.ManageEntityVo;
import com.ship.dispatch.mapper.fob.SpFobGmbxMapper;
import com.ship.dispatch.mapper.fob.SpfobJsfhccbMapper;
import com.ship.dispatch.service.ConShippingService;
import com.ship.dispatch.service.ShipDashboarDataService;
import com.ship.dispatch.service.SpDispatchSchemeExecuteService;
import com.ship.dispatch.service.fob.*;
import com.ship.dispatch.util.StringUtils;
import org.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yl
 * @title SpfobJsfhccbServiceImpl
 * @description 计算往返航次成本用于报关
 * @create 2023/12/18 0018
 */
@Service
public class SpfobJsfhccbServiceImpl extends FobBaseServiceImpl<SpfobJsfhccbMapper,SpfobJsfhccb> implements SpfobJsfhccbService {

    @Autowired(required = false)
    SpfobJsfhccbMapper spFobGmbxMapper;

    @Autowired
    SpFobFileRecordService spFobFileRecordService;

    @Override
    public void updateEntity(ManageEntityVo manageEntityVo, String json) {
        SpfobJsfhccb object = JsonUtils.jsonToObject(json,SpfobJsfhccb.class);
        SpfobJsfhccb his = spFobGmbxMapper.selectOne(new LambdaQueryWrapper<SpfobJsfhccb>().eq(SpfobJsfhccb::getProceeInstanceId,manageEntityVo.getProcessInstanceId()).ne(SpfobJsfhccb::getStatus,-2));
        if(manageEntityVo.getOperationType()==1) {
            object.setStatus(1);
        }
        if(his==null){ // 新增
            object.setSchemeDetailId(manageEntityVo.getSchemeDetailId());
            object.setTaskId(manageEntityVo.getTaskId());
            object.setProceeInstanceId(manageEntityVo.getProcessInstanceId());
            spFobGmbxMapper.insert(object);
        }else { // 修改
            object.setId(his.getId());
            BeanUtils.copyProperties(object,his);
            spFobGmbxMapper.updateById(his);
            // 处理多余文件记录
            List<String> ids = new ArrayList<>();
            if((his.getFiels()!=null && his.getFiels().length()>0)){
                String temp = his.getFiels();
                String[] fieldIds = temp.split(",");
                ids =  Arrays.stream(fieldIds).collect(Collectors.toList());
            }
            spFobFileRecordService.deleteOtherFileRecor(his.getId(),ids);
        }
    }

    @Override
    public SpFobBaseBean getBaseBeanByInstanceId(String id) {
        return spFobGmbxMapper.selectOne(new LambdaQueryWrapper<SpfobJsfhccb>().eq(SpfobJsfhccb::getProceeInstanceId,id).ne(SpfobJsfhccb::getStatus,-2));
    }
    @Override
    public void  saveEntity(SpFobBaseBean bean){
        SpfobJsfhccb spFobZzgcjr = new SpfobJsfhccb();
        BeanUtils.copyProperties(bean,spFobZzgcjr);
        this.save(spFobZzgcjr);
    }

    @Override
    public void updateEntityById(SpFobBaseBean bean) {
        SpfobJsfhccb spFobZzgcjr = new SpfobJsfhccb();
        BeanUtils.copyProperties(bean,spFobZzgcjr);
        this.updateById(spFobZzgcjr);
    }


    @Override
    public Object getFormData(String id,Long serviceId) {
        SpfobJsfhccb spFobZzgcjr = null;
        if(serviceId!=null){
            spFobZzgcjr = spFobGmbxMapper.selectById(serviceId);
        }else {
            spFobZzgcjr = spFobGmbxMapper.selectOne(new LambdaQueryWrapper<SpfobJsfhccb>().eq(SpfobJsfhccb::getProceeInstanceId,id).ne(SpfobJsfhccb::getStatus,-2));
        }
        if(spFobZzgcjr==null){
            return null;
        }
        Map<String,Object> map = JsonUtils.jsonToMap(JsonUtils.objectToJson(spFobZzgcjr), Object.class);
        if(StringUtils.isNotBlank(spFobZzgcjr.getFiels())){
            String[] ids = spFobZzgcjr.getFiels().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordList =  spFobFileRecordService.listByIds(resId);
            map.put("fiels",spFobFileRecordList);
        }
        return map;
    }
    @Override
    public List<Long> findServieData(String processInstanceId){
        List<SpfobJsfhccb> spFobBaseBeans =  spFobGmbxMapper.selectList(new LambdaQueryWrapper<SpfobJsfhccb>().eq(SpfobJsfhccb::getProceeInstanceId,processInstanceId));
        List<Long> list = spFobBaseBeans.stream().map(SpFobBaseBean::getId).collect(Collectors.toList());
        return list;
    }

    @Autowired
    ShipDashboarDataService shipDashboarDataService;
    @Autowired
    SpDispatchSchemeExecuteService schemeDetailService;

    @Autowired
    ConShippingService conShippingService;
    /**
     * 完成任务推送信息
     * @param id
     */

    @Autowired
    SpFobSyzlzzwjService spFobSyzlzzwjService;

    @Autowired
    SpFobQrxzgService spFobQrxzgService;

    @Autowired
    SpFobGczfstdService spFobGczfstdService;

    @Autowired
    HistoryService historyService;
    @Override
    public void createPayOrder(Long id) throws IOException {
        SpfobJsfhccb his = this.getById(id);
        HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(his.getProceeInstanceId()).singleResult();
        if(processInstance==null || !"FOB001v1".equals(processInstance.getProcessDefinitionKey())){
            return;
        }
        SpDispatchSchemeExecute execute = schemeDetailService.getExceuteByInstanceId(his.getProceeInstanceId());
        ShipDashboarDataRq shipDashboarDataRq = new ShipDashboarDataRq();
        shipDashboarDataRq.setBuyer(execute.getBuyer());
        shipDashboarDataRq.setVoyageNo(execute.getVoyageNo());
        shipDashboarDataRq.setGoodsNum(execute.getGoodsNum());
        shipDashboarDataRq.setSeller(execute.getSeller());
        shipDashboarDataRq.setTradeType(execute.getTradeType()+"");
        shipDashboarDataRq.setBuyer(execute.getBuyer());
        shipDashboarDataRq.setShipName(execute.getShipName());
        shipDashboarDataRq.setImo(execute.getImo());
        shipDashboarDataRq.setShipTradeBody(execute.getShipTradeBody()+"");
        shipDashboarDataRq.setShipContractType(execute.getShipContractType()+"");
        shipDashboarDataRq.setExpectedLoadingPort(execute.getExpectedLoadingPort());
        shipDashboarDataRq.setLoadingCapacity(execute.getLoadingCapacity());
        shipDashboarDataRq.setRealityLoadingCapacity(execute.getRealityLoadingCapacity());
        shipDashboarDataRq.setExpectedLoadingDate(execute.getExpectedLoadingDate());
        shipDashboarDataRq.setExpectedLoadingCompletionWindow(execute.getExpectedLoadingCompletionWindow());
        shipDashboarDataRq.setExpectedFullLoadVoyageDays(execute.getExpectedFullLoadVoyageDays());
        SpFobSyzlzzwj spFobSyzlzzwj = spFobSyzlzzwjService.findActivitiData(his.getProceeInstanceId());
        if(spFobSyzlzzwj!=null){
            shipDashboarDataRq.setRealityLoadingDate(spFobSyzlzzwj.getLoadingStartDate());
            shipDashboarDataRq.setLoadingEndDate(spFobSyzlzzwj.getLoadingEndDate());
        }
        SpFobQrxzg spFobQrxzg = spFobQrxzgService.findData(his.getProceeInstanceId());
        if(spFobQrxzg!=null){
            shipDashboarDataRq.setExpectedUnloadingPort(spFobQrxzg.getExpectedUnloading());
        }
        shipDashboarDataRq.setContractUnloadingCapacity(execute.getContractUnloadingCapacity());
        shipDashboarDataRq.setRealityUnloadingCapacity(execute.getRealityUnloadingCapacity());
        shipDashboarDataRq.setExpectedUnloadingWindow(execute.getExpectedUnloadingWindow());
        shipDashboarDataRq.setExpectedUnloadingCompletionWindow(execute.getExpectedUnloadingCompletionWindow());
        //shipDashboarDataRq.setRealityUnloadingDate(execute.getRealityUnloadingDate());
        shipDashboarDataRq.setExpectedEmptyLoadVoyageDays(execute.getExpectedEmptyLoadVoyageDays());
        shipDashboarDataRq.setExpectedEmptyReturnWindow(execute.getExpectedEmptyReturnWindow());
        SpFobGczfstd spFobGczfstd = spFobGczfstdService.findDate(his.getProceeInstanceId());
        if(spFobGczfstd!=null){
            shipDashboarDataRq.setTdNo(spFobGczfstd.getTdNo());
        }
        shipDashboarDataService.pushGuanWuData(shipDashboarDataRq);
    }
}
